<?php
require('fpdf.php');

class PDF extends FPDF
{
//Load data
function LoadData($file)
{
    //Read file lines
    $lines=file($file);
    $data=array();
    foreach($lines as $line)
        $data[]=explode(';',chop($line));
    return $data;
}

//Colored table
function FancyTable($header,$data)
{
    //Colors, line width and bold font
    $this->SetFillColor(32,98,184);
    $this->SetTextColor(255);
    $this->SetDrawColor(0,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
	$this->SetX(0);
    //Header
    $w=array(40,40,22,22,22,22,22,20);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    //Color and font restoration
    $this->SetFillColor(228,235,245);
    $this->SetTextColor(0);
    $this->SetFont('');
	$this->SetFontSize('8');
	
    //Data
    $fill=false;
    foreach($data as $row)
    {
		$this->SetX(0);
        $this->Cell($w[0],6,substr($row[0], 0, 25),'LR',0,'L',$fill);
        $this->Cell($w[1],6,substr($row[1], 0, 25),'LR',0,'L',$fill);
        $this->Cell($w[3],6,substr($row[3], 0, 14),'LR',0,'L',$fill);
		$this->Cell($w[3],6,substr($row[4], 0, 14),'LR',0,'L',$fill);
		$this->Cell($w[3],6,substr($row[5], 0, 14),'LR',0,'L',$fill);
		$this->Cell($w[3],6,substr($row[8], 0, 14),'LR',0,'L',$fill);
		$this->Cell($w[3],6,substr($row[9], 0, 14),'LR',0,'L',$fill);
		$this->Cell($w[3],6,substr($row[10], 0, 14),'LR',0,'L',$fill);
        $this->Ln();
		$this->SetX(0);
        $fill=!$fill;
    }
    $this->Cell(array_sum($w),0,'','T');
}
	//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    $this->Cell(0,10,'Deze pagina werd gegenereert met Kebo ' . date("d/m/Y H:i"),0,0,'C');
}
}

if(isset($_GET) && isset($_POST)) 
{
	$n = $_GET["rows"];
	$table = "";
	for($i=1; $i<$n; $i++) 
	{
		for($j=0; $j<12; $j++) 
		{
			$table .= $_POST["element$i$j"];
			$table .= ";";
		}
		$table .= "\r\n";
	}
	$fp = fopen("txt/exportTable.txt", "w+");
	fwrite($fp, $table);
	fclose($fp);
	
	$pdf=new PDF();
	//Column titles
	$header=array('Titel', 'Auteur', 'Genre', 'Thema1', 'Thema2', 'Uitgeverij', 'Plaats', 'Materiaal');
	
	//Data loading
	$data=$pdf->LoadData('txt/exportTable.txt');
	$pdf->SetFont('Arial','',8);
	$pdf->SetSubject('Export');
	$pdf->SetAuthor('Kebo');
	$pdf->AddPage();
	$pdf->FancyTable($header,$data);
	$pdf->Output();
	
}
?>